package cn.practice.chat.handler;


import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;


/**
 * @ClassName WebSocketChannelHandler
 * @Description:
 * @Author A-TAO
 * @Date 2022/10/30
 * @Version V1.0
 **/
public class WebSocketChannelHandler extends ChannelInitializer<SocketChannel> {

    //初始化通道
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        // 获取管道 将一个个ChannelHandler添加到管道
        ChannelPipeline pipeline = socketChannel.pipeline();
        // 添加一个http编码器
        pipeline.addLast(new HttpServerCodec());
        // 添加一个用于支持大数据流的支持
        pipeline.addLast(new ChunkedWriteHandler());
        // 添加一个聚合器，主要是将HttpMessage聚合成FullHttpRequest/Response
        pipeline.addLast(new HttpObjectAggregator(1024 * 64));
        pipeline.addLast(new AuthTokenHandler());
        // Netty支持websocket
        //pipeline.addLast(new WebSocketServerProtocolHandler("/ws", null, false));
        // 自定义
        pipeline.addLast(new WebSocketHandler());
    }
}
